package kids.movies.hoathinhHDSRC.fragment;

import java.util.ArrayList;

import kids.movies.hoathinhHD.R;
import kids.movies.hoathinhHDSRC.BaseFragment;
import kids.movies.hoathinhHDSRC.E4KidsConfig;
import kids.movies.hoathinhHDSRC.E4KidsSlidingMenuActivity;
import kids.movies.hoathinhHDSRC.KidsTVApplication;
import kids.movies.hoathinhHDSRC.adapter.ItemCatAdapter;
import kids.movies.hoathinhHDSRC.db.DbSupport;
import kids.movies.hoathinhHDSRC.db.InfoOj;
import kids.movies.hoathinhHDSRC.db.TableDb;
import kids.movies.hoathinhHDSRC.service.RequestService;
import kids.movies.hoathinhHDSRC.util.Debug;
import kids.movies.hoathinhHDSRC.util.ViewUtil;
import kids.movies.hoathinhHDSRC.view.CatDetailActivity;
import kids.movies.hoathinhHDSRC.view.IndexNoTabActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bzydroid.network.http.AbsRequestHandler;
import com.bzydroid.network.http.NetworkResponse;
import com.bzydroid.network.http.client.BzydroidHttpClient;
import com.bzydroid.network.http.exception.BzydroidNetworkExeception;
import com.bzydroid.network.http.response.HttpJSONObjectResponse;
import com.bzydroid.utils.BzydroidLog;
import com.google.android.gms.internal.da;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;

public class SlideLeftFragment extends BaseFragment {

	private ItemCatAdapter adapter;
	private ListView listView;
	private ScrollView scrollView;
	private View vSelectedIndex;

	private TextView tvIndex;
	private TextView tvInfo;

	private View root;
	private KidsTVApplication application;
	private ArrayList<InfoOj> datas;
	private E4KidsSlidingMenuActivity activity;
	private Dialog dialog;

	public static SlideLeftFragment newInstance() {
		return new SlideLeftFragment();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.layout_slide_listcat, null);
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		activity = (E4KidsSlidingMenuActivity) getActivity();
		listView = (ListView) root.findViewById(R.id.lvListCat);
		scrollView = (ScrollView) root.findViewById(R.id.scrollView);
		vSelectedIndex = root.findViewById(R.id.vSelectedIndex);
		tvIndex = (TextView) root.findViewById(R.id.tvIndex);
		tvInfo = (TextView) root.findViewById(R.id.tvInfo);

		application = (KidsTVApplication) getActivity().getApplication();
		
		adapter = new ItemCatAdapter(getActivity(), application.getCatID());
		listView.setAdapter(adapter);
		
		adapter.setData(DbSupport.getCache(TableDb.CATEGORY));
		
		
		ViewUtil.setListViewHeightBasedOnChildren(listView);

		if (application.getCatID() == -1) {
			vSelectedIndex.setVisibility(View.VISIBLE);
			tvIndex.setBackgroundColor(getResources().getColor(
					R.color.color_bg_slide_selected));

		} else {
			vSelectedIndex.setVisibility(View.GONE);
			tvIndex.setBackgroundColor(getResources().getColor(
					R.color.color_bg_slide));
		}

		tvIndex.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				application.setCatID(-1);
				if (!application.isIndex()) {
					application.setIndex(true);
					Intent intent = new Intent(getActivity(),
							IndexNoTabActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				} else {
					application.setIndex(true);
					activity.getSlidingMenu().toggle();
				}

			}
		});

		tvInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Debug.debug("CLick Info : ");
				showDialog();
				activity.getSlidingMenu().toggle();
			}
		});

		

		// scrollView.fullScroll(ScrollView.FOCUS_UP);
		scrollView.smoothScrollTo(0, 0);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				InfoOj iOj=adapter.getItem(arg2);
				int vitri=iOj.getInt(InfoOj.ID_CATEGORY);
				Debug.debug("vi tri" +vitri);
				if (vitri == application
						.getCatID()) {
					activity.getSlidingMenu().toggle();
				} else {
					application.setCatID(vitri);

					Intent intent = new Intent(getActivity(),
							CatDetailActivity.class);
					if (!application.isIndex()) {
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						application.setIndex(false);
						startActivity(intent);
					} else {
						application.setIndex(false);
						startActivity(intent);
					}
				}
			}
		});
		RequestService.rqListCat(packageName, new RequestListCat());
	}

	private class RequestListCat extends AbsRequestHandler {
		@Override
		public void onStarted(BzydroidHttpClient arg0) {
			super.onStarted(arg0);
			BzydroidLog.debug("onStarted");
		}

		@Override
		public void onSuccess(BzydroidHttpClient arg0, NetworkResponse arg1) {

			HttpJSONObjectResponse jsonObjectResponse = (HttpJSONObjectResponse) arg0
					.getResponse();

			Debug.debug("Danh sach cat : " + jsonObjectResponse.toString());
			try {
				JSONObject object = jsonObjectResponse.toJSONObject();

				JSONObject jObjData = object
						.getJSONObject(E4KidsConfig.KEY_DATA);

				int code = jObjData.getInt(E4KidsConfig.KEY_CODE);

				if (code == 1) {
					JSONArray jArrCat = jObjData
							.getJSONArray(E4KidsConfig.KEY_LISTCAT);
					ArrayList<BaseObject> ojsilde = JsonSupport
							.jsonArray2BaseOj(jArrCat);
					DbSupport.saveCache(ojsilde, TableDb.CATEGORY);
					adapter.setData(DbSupport.convertOj(ojsilde));

					ViewUtil.setListViewHeightBasedOnChildren(listView);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onError(BzydroidHttpClient arg0,
				BzydroidNetworkExeception arg1) {
			super.onError(arg0, arg1);
			BzydroidLog.debug("onError");
		}

		@Override
		public void onFinished(BzydroidHttpClient arg0) {
			super.onFinished(arg0);
			BzydroidLog.debug("onFinished");
		}

	}

	private void showDialog() {

		if (dialog == null) {
			dialog = new Dialog(getActivity());
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		}

		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(R.layout.layout_info_app);

		dialog.show();
	}

	private void hidenDialog() {
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
	}
}
